<?php
/**
 * 
 * Class is intended to ease programatic jQuery UI Tabs widget creation
 * 
 * @author Alton Crossley <crossleyframework@nogahidebootstrap.com>
 * @package Crossley Framework
 *  
 * @copyright Copyright (c) 2003-2009, Nogaide BootStrap INC. All rights reserved.
 * @license BSD http://opensource.org/licenses/bsd-license.php
 * @version $Id:$
 * 
 */
class X_Dom_Jquery_Tabs extends XMLWriter
{
	/**
	 * message that is displayed if there is an error loading a tab via Ajax
	 * @var string
	 */
	public $sAjaxErrorMessage = "Couldn't load this tab. We'll try to fix this as soon as possible.";
	/**
	 * 2D Associative array of tab strings
	 * format:
	 * array(
	 * 	array(
	 * 		"title" => "Simple pre rendered text"
	 *      ,"content" => "<p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus.</p>"
	 *  )
	 *  ,array(
	 * 		"title" => "Ajax Loaded"
	 *      ,"URL" => "http://example.com/stuff"
	 *  )
	 * );
	 * 
	 * @var array
	 */
	private $_aTabs = array();
	/**
	 * Associative array that contains temparary tab content
	 * 
	 * @var array
	 */
	private $_aContent = array();
	/**
	 * tracks weather we are using ajax
	 * @var bool
	 */
	private $_bUsesAjax = false;
	/**
	 * stores the current container div ID
	 * 
	 * @var string
	 */
	private $sId = 'x_tabs';
	/**
	 * options passed to jquery ui tabs
	 * 
	 * @var array
	 */
	private $_aUiOptions = array();
	/**
	 * simple construction to wrap XMLWriter init
	 * 
	 * @param string $sID
	 * @param array $aAttributes key value pair of attributes
	 */
	function __construct($sID = 'x_tabs', $aAttributes = array())
    {
    	$this->sId = $sID;
        $this->openMemory();
        $this->startElement('div');
        $this->writeAttribute('id', $sID);
        foreach ($aAttributes as $sName => $sValue)
        {
        	$this->writeAttribute($sName, $sValue);
        }
    }
    /**
     * return the DOM id of the tab markup
     * 
     * @return string
     */
    public function getId()
    {
    	return $this->sId;
    }
    /**
     * set an option to be passed to tabs on client creation
     * @param string $sKey
     * @param string $sValue
     */
    public function setOption($sKey, $sValue)
    {
    	$this->_aUiOptions[$sKey] = (string)$sValue;
    	return $this;
    }
    /**
     * get the options to be passed to jquery ui
     * 
     * @return array
     */
    public function getOptions()
    {
    	return $this->_aUiOptions;
    }
    /**
     * set the options to be passed to jquery ui
     * 
     * @param array $aOptions
     * @param bool $bMerge
     */
    public function setOptions($aOptions, $bMerge = true)
    {
    	if ($bMerge)
    	{
    		$this->_aUiOptions = array_merge($this->_aUiOptions, $aOptions);    		
    	}
    	else
    	{
    		$this->_aUiOptions = $aOptions;
    	}
    	return $this;
    }
    /**
     * add a tab that contains prerendered content
     * 
     * @param string $sTitle
     * @param string $sMarkup
     */
    public function addRenderedTab($sTitle, $sMarkup)
    {
    	$this->_aTabs[] = array(
    		"title" => $sTitle
    		,"content" => $sMarkup
    	);
    	return $this;
    }
    /**
     * add a tab that will be loaded via ajax
     * 
     * @param string $sTitle
     * @param string $sUrl
     */
	public function addAjaxTab($sTitle, $sUrl)
    {
    	$this->_aTabs[] = array(
    		"title" => $sTitle
    		,"URL" => $sUrl
    	);
    	return $this;
    }
    
    private function _renderHead()
    {
    	$this->startElement('li');
    	foreach ($this->_aTabs as $iPos => $aTab)
    	{
    		if (array_key_exists('URL', $aTab))
    		{
    			$sHref = $aTab['URL'];
    		}
    		else
    		{
    			$sHref = '#tab-' . $iPos;
    			$sContent = (array_key_exists('content', $aTab)) ?
    				$aTab['content'] :
    				'&nbsp;';
    			$this->_registerTabContent($sHref, $sContent);    			
    		}
    		$this->_addHeadLi($aTab['title'], $sHref);
    	}
    	$this->endElement();
    	return $this;
    }
    
    private function _registerTabContent($sId, $sContent)
    {
    	$this->_aContent[$sId] = $sContent;
    	return $this;
    }
    
    private function _addHeadLi($sTitle, $sHref)
    {
    	$this->startElement('li');
	    	$this->startElement('a');
	    		$this->writeAttribute('href', $sHref);
	    		$this->writeRaw($sTitle);
	    	$this->endElement();
    	$this->endElement();
    	return $this;
    }
    
    private function _renderBody()
    {
    	foreach ($this->_aContent as $sId => $sContent)
    	{
    		$this->_addDiv($sId, $sContent);
    	}
    	return $this;
    }
    
    private function _addDiv($sId, $sContent)
    {
    	$this->startElement('div');
    		$this->writeAttribute('id', $sId);
    		$this->writeRaw($sContent);
    	$this->endElement();
    	return $this;
    }
    
    /**
     * simple string conversion wrapping needed methods
     * 
     * @return string
     */
	function __toString()
    {
        $this->_renderHead();
        $this->_renderBody();
        
        $this->endElement(); 
        $this->endDocument(); 
        return $this->flush();         
    }
}